﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace CodeBadger.GasMileage.Converters
{
    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var date = (DateTime)value;
                return parameter == null 
                    ? date.ToString(culture) 
                    : date.ToString(parameter as string, culture);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var valueAsString = value as string;
            return valueAsString != null 
                ? (object) DateTime.Parse(valueAsString, culture) 
                : null;
        }
    }
}
